// pages/testpage/testpage.js

// if (!global.__minium_request_stack) {
//   global.__minium_request_stack = new Map();
//   global.__minium_wait_util = function(cnt, timeout=20) {
//       var pageId = getCurrentPages().pop().__wxWebviewId__;
//       return new Promise((resolve) => {
//           var loop = timeout * 10
//           var fd = setInterval(() => {
//               loop--
//               var size = global.__minium_request_stack.has(pageId) ? global.__minium_request_stack.get(pageId).size : 0
//               if (size <= cnt) {
//                   clearInterval(fd)
//                   resolve(true)
//               } else if (loop < 0) {
//                   clearInterval(fd)
//                   resolve(false)
//               }
//           }, 100)
//       })
      
//   }
//   var origin = wx.request;
//   Object.defineProperty(wx, "request", {
//       configurable: true,
//       get() {
//           return function (obj) {
//               if (obj.url == "https://weapp.tencent.com/jscov_driver/CollectCovTimer"){
//                   return origin(obj);
//               }
//               var pageId = getCurrentPages().pop().__wxWebviewId__;
//               var requestMap
//               if (global.__minium_request_stack.has(pageId)) {
//                   requestMap = global.__minium_request_stack.get(pageId);
//               } else {
//                   requestMap = new Map();
//                   global.__minium_request_stack.set(pageId, requestMap);
//               }
//               var complete = obj.complete;
//               var requestId;
//               obj.complete = (res) => {
//                   requestMap.delete(requestId)
//                   if (requestMap.size === 0) global.__minium_request_stack.delete(pageId)
//                   complete && complete(res);
//               }
//               var requestTask = origin(obj);
//               requestId = requestTask.uniqueId || Math.random()
//               requestMap.set(requestId, Date.now())
//               return requestTask
//           }
//       }
//   })
// }

// wx.onAppRouteDone(() => {
//   console.warn("[minitest] route done")
//   global.__minium_wait_util(2, 10).then(d => {
//     console.warn("wait util ", d)
//   })

// })

const g = (function () {
    return this
})

const app = getApp()
app.globalData.ggg = g
app.globalData.gggthis = globalThis

function before() {
    app.globalData.gggthis2 = globalThis
}

before()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    "testdata1": 1,
    "waitAsync": 0
  },

  onLoad() {
    
    for (let i=0; i < 10; i++) {
      wx.request({
        url: 'https://stream.weixin.qq.com/weapp/SendMsg?content=10&sleep_time=3',
        success: () => {
          console.warn("[minitest] request succ")
          this.setData({
            waitAsync: i
          })
        }
      })
    }
  },

  callMethod(e) {
    e.message = 'this is test callMethod';
    return e;
  },

  testWaitFor() {
    this.setData({
      testdata1: 0,
    });
    setTimeout(() => {
      this.setData({
        testdata1: 1,
      });
    }, 2000);
  },

  testWaitData() {
    delete this.data.testwait;
    delete this.data.waitkeys;
    setTimeout(() => {
      this.setData({
        testwait: 1,
        waitkeys: { a: 1, b: false },
      });
    }, 2000);
    setTimeout(() => {
      delete this.data.testwait;
      delete this.data.waitkeys;
    }, 5000);
  },

  userNameInput(e) {
    this.setData({
      username: e.detail.value,
    });
  },

  testfocus(e) {
    console.log('input focus: ', e);
  },
});

